/* Copyright 2012 Dustin Mitchell <dmmitche [at] gmail [dot] com>
 *
 * Licensed under the GPLv3.  See included LICENSE.txt for full details.
 */

#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "pin_mux.h"
#include "sysfs.h"

#ifdef ARM
# define MUX_PATH_PREFIX "/sys/kernel/debug/omap_mux"
#else
# define MUX_PATH_PREFIX "/dev"
#endif

#define NUM_PINS 66


int pin_mux_set_mode (const char *pin, enum pin_mux_mode mode)
{
  char mux_path[PATH_MAX];
  int mux_fd;

  if (!pin)
    return -1;

  snprintf (mux_path, PATH_MAX, MUX_PATH_PREFIX "/%s", pin);

  mux_fd = open (mux_path, O_WRONLY);
  if (mux_fd == -1)
    return -1;

  if (sysfs_write_int (mux_fd, mode) == -1) {
    close (mux_fd);
    return -1;
  }

  close (mux_fd);

  return 0;
}
